package cn.buk.tms.eotms.controller;

import cn.buk.common.dto.CommonDto;
import cn.buk.common.dto.eterm.PnrDto;
import cn.buk.common.dto.eterm.PnrFlightDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.eotms.dto.TicketSaleDto;
import cn.buk.tms.eotms.entity.Airport;
import cn.buk.tms.eotms.entity.DetrInfo;
import cn.buk.tms.eotms.service.BasicDataService;
import cn.buk.tms.eotms.service.FlightService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yfdai
 * @date 2016-12-3
 */
@RestController
public class FlightController extends BaseController {

    private static Logger logger = Logger.getLogger(FlightController.class);

    @Autowired
    private FlightService flightService;

    @Autowired
    private BasicDataService basicDataService;

    @RequestMapping(value="/tprs")
    public CommonDto<TicketSaleDto> searchTprs(HttpServletRequest request,
                                               @RequestParam(value="beginDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") final Date beginDate,
                                               @RequestParam(value="endDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") final Date endDate,
                                               @RequestParam(value = "device", defaultValue = "0") final int deviceId,
                                               @RequestParam(value = "ticketNo", required = false) String ticketNo,
                                               @RequestParam(value = "etermUsername", required = false) String etermUsername,
                                               @RequestParam(value = "pageSize", defaultValue = "1000") final int pageSize,
                                               @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo) {
        CommonSearchCriteria sc = new CommonSearchCriteria();
        sc.setPageNo(pageNo);
        sc.setPageSize(pageSize);

        List<TicketSaleDto> list =  flightService.searchTprs(getUserId(request), beginDate, endDate, etermUsername, deviceId, ticketNo, sc);

        CommonDto<TicketSaleDto> tsDto = new CommonDto<>();
        tsDto.setDataList(list);
        tsDto.setPage(sc.getPage());

        return tsDto;
    }

    @RequestMapping("/detrs")
    public CommonDto<DetrInfo> searchDetrs(HttpServletRequest request,
                                                @ModelAttribute("sc") CommonSearchCriteria sc) {
        List<DetrInfo> detrInfos = flightService.searchDetrs(getUserId(request), sc);

        CommonDto<DetrInfo> commonDto  = new CommonDto<>();
        commonDto.setDataList(detrInfos);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    @RequestMapping("/processPnrDetail.do")
    public PnrDto processPnrDetail(HttpServletRequest request,
                                   @RequestParam("pnrDetail") String pnrDetail) {
        final int userId = getUserId(request);

        PnrDto pnrInfo = new PnrDto();
        pnrInfo.setPnrDetail(pnrDetail);

        Set<String> airportSet = new HashSet<>();
        for (PnrFlightDto flt : pnrInfo.getFlights()) {
            airportSet.add(flt.getFlight().getDport());
            airportSet.add(flt.getFlight().getAport());
        }

        //默认为国内
        pnrInfo.setType3(0);
        for(String threeCode: airportSet) {
            Airport airport = basicDataService.getAirportByCode(threeCode);
            if (airport == null) {
                pnrInfo.setType3(-1);
                break;
            }

            if ("CN".equalsIgnoreCase(airport.getCountryCode())) {
                // 国内机场
            } else {
                // 国际机场
                pnrInfo.setType3(1);
                break;
            }
        }


        try {
//            pnrService.savePnrLog(enterpriseId, pnrInfo.getPnrNo(), pnrDetail, "分析PNR");
            logger.info(userId + ", " + pnrInfo.getPnrNo());
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }

        return pnrInfo;
    }


}
